{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               学号   姓名 性别\n",
       "0  20000502310022  叶庭云  男\n",
       "1  20000502310052   锐雯  女\n",
       "2  20000581310080   亚索  男\n",
       "3  20001683310012   李青  男\n",
       "4  20001683310026  贾克斯  男"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>学号</th>\n      <th>姓名</th>\n      <th>性别</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20000502310022</td>\n      <td>叶庭云</td>\n      <td>男</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>20000502310052</td>\n      <td>锐雯</td>\n      <td>女</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>20000581310080</td>\n      <td>亚索</td>\n      <td>男</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>20001683310012</td>\n      <td>李青</td>\n      <td>男</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>20001683310026</td>\n      <td>贾克斯</td>\n      <td>男</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "# 掌握了基本使用之后，我们就能利用批量操作来解放双手（记得补充安装一个 xlrd 的库，否则会报错）\n",
    "import pypinyin\n",
    "import pandas as pd\n",
    "\n",
    "# 先导入一份学生名单如下\n",
    "df = pd.read_excel('学生名单_test.xls')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Ye Tingyun 叶庭云\nRui Wen 锐雯\nYa Suo 亚索\nLi Qing 李青\nJia Kesi 贾克斯\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               学号   姓名 性别         英文名 拼音首字母\n",
       "0  20000502310022  叶庭云  男  Ye Tingyun   YTY\n",
       "1  20000502310052   锐雯  女     Rui Wen    RW\n",
       "2  20000581310080   亚索  男      Ya Suo    YS\n",
       "3  20001683310012   李青  男     Li Qing    LQ\n",
       "4  20001683310026  贾克斯  男    Jia Kesi   JKS"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>学号</th>\n      <th>姓名</th>\n      <th>性别</th>\n      <th>英文名</th>\n      <th>拼音首字母</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20000502310022</td>\n      <td>叶庭云</td>\n      <td>男</td>\n      <td>Ye Tingyun</td>\n      <td>YTY</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>20000502310052</td>\n      <td>锐雯</td>\n      <td>女</td>\n      <td>Rui Wen</td>\n      <td>RW</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>20000581310080</td>\n      <td>亚索</td>\n      <td>男</td>\n      <td>Ya Suo</td>\n      <td>YS</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>20001683310012</td>\n      <td>李青</td>\n      <td>男</td>\n      <td>Li Qing</td>\n      <td>LQ</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>20001683310026</td>\n      <td>贾克斯</td>\n      <td>男</td>\n      <td>Jia Kesi</td>\n      <td>JKS</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "# 下面是使用 `pypinyin` 批量将全部学生的中文姓名转换为拼音\n",
    "pinyin_name = []\n",
    "first_pinyin = []\n",
    "for i in df['姓名']:\n",
    "    result = pypinyin.pinyin(i, style=pypinyin.NORMAL)\n",
    "    result_ = [i[0] for i in result]\n",
    "    result2 = result_[0].capitalize() + ' ' + ''.join(result_[1:]).capitalize()\n",
    "    result3 = ''.join([i[0].upper() for i in result_])\n",
    "    print(result2, i, sep=' ')\n",
    "    pinyin_name.append(result2)\n",
    "    first_pinyin.append(result3)\n",
    "    \n",
    "df['英文名'] = pinyin_name\n",
    "df['拼音首字母'] = first_pinyin\n",
    "df.head()"
   ]
  }
 ]
}